package com.frame.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.frame.common.core.entity.SysNotice;
import com.frame.common.core.security.utils.SecurityUtils;
import com.frame.db.util.Condition;
import com.frame.db.util.Query;
import com.frame.stant.utils.R;
import com.frame.system.service.ISysNoticeService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 公告 信息操作处理
 *
 * @author ruoyi
 */
@Api(tags = "公告")
@RestController
@RequestMapping("/system/notice")
@AllArgsConstructor
public class SysNoticeController  {

    private final ISysNoticeService noticeService;

    /**
     * 获取通知公告列表
     */
    @PreAuthorize("@custom.hasAnyAuthority('system:notice:list')")
    @GetMapping("/list")
    public R list(SysNotice notice, Query query) {
        IPage<SysNotice> list = noticeService.selectNoticeList(Condition.page(query),notice);
        return R.success(list);
    }

    /**
     * 根据通知公告编号获取详细信息
     */
    @PreAuthorize("@custom.hasAnyAuthority('system:notice:query')")
    @GetMapping(value = "/{noticeId}")
    public R getInfo(@PathVariable Long noticeId) {
        return R.success(noticeService.getById(noticeId));
    }

    /**
     * 新增通知公告
     */
    @PreAuthorize("@custom.hasAnyAuthority('system:notice:add')")
//    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@Validated @RequestBody SysNotice notice) {
        notice.setCreateBy(SecurityUtils.getUsername());
        return R.success(noticeService.save(notice));
    }

    /**
     * 修改通知公告
     */
    @PreAuthorize("@custom.hasAnyAuthority('system:notice:edit')")
//    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@Validated @RequestBody SysNotice notice) {
        notice.setUpdateBy(SecurityUtils.getUsername());
        return R.success(noticeService.updateById(notice));
    }

    /**
     * 删除通知公告
     */
    @PreAuthorize("@custom.hasAnyAuthority('system:notice:remove')")
//    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{noticeIds}")
    public R remove(@PathVariable List<Long> noticeIds) {
        return R.success(noticeService.removeByIds(noticeIds));
    }
}
